package cn.boot1112.spingboot.controller;

import cn.boot1112.spingboot.Mapper.UserMapper;
import cn.boot1112.spingboot.entity.User;
import cn.boot1112.spingboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 路径：/login
 * 参数：username password
 * 返回：JsonResult
 * 业务：查询用户是否为null:
 *              是：state设置为2
 *              否：用户名正确
 *                  判断查询结果的密码和参数password是否匹配
 *                      是：state设置为1
 *                      否：state设置为3
 */

@RestController
public class UserController {
    @Autowired
    UserMapper  userMapper;
    @RequestMapping("login")
    public JsonResult login(String username, String password){
        //创建JsonResult对象
        JsonResult jsonResult = new JsonResult();
        //根据参数username查询用户
        User result = userMapper.findByUsername(username);
        if(result == null){
            jsonResult.setState(2);

        }else {
            //判断密码是否一致
            if(result.getPassword().equals(password)){
                jsonResult.setState(1);
            }
            else{
                jsonResult.setState(3);
            }
        }
        return jsonResult;


    }
}
